package com.example.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.project.entity.*;
import com.example.project.mapper.*;
import com.example.project.service.ConFilesService;
import com.example.project.service.FileService;
import com.example.project.vo.model.PageDto;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.ArrayList;
import java.util.List;

/**
 * (ConFiles)表服务实现类
 */
@Service("conFilesService")
public class ConFilesServiceImpl extends ServiceImpl<ConFilesMapper, ConFiles>
        implements ConFilesService {
    @Resource
    private ConFilesMapper conFilesMapper;

    @Resource
    private FileMapper fileMapper;

    @Resource
    private DepartMapper departMapper;

    @Resource
    private ProjectMapper projectMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private FileService fileService;



    @Override
    public ConFiles queryById(Long id) {
        ConFiles files = this.getBaseMapper().selectById(id);
        dealConFile(files);
        return files;
    }

    @Override
    public PageInfo<ConFiles> queryAll(PageDto pageDto) {
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<ConFiles> list = this.getBaseMapper().selectList(pageDto.toQueryWrapper());
        list.forEach(files -> {
            dealConFile(files);
        });
        return new PageInfo<>(list);
    }

    @Override
    public ConFiles insert(ConFiles conFiles) {
        Project project = projectMapper.selectById(conFiles.getProjId());
        String projName = project.getProjName();

        User user = userMapper.selectById(conFiles.getUserId());
        Long depId = user.getDepId();
        conFiles.setDeptId(depId);

        this.getBaseMapper().insert(conFiles);
        return conFiles;
    }

    @Override
    public ConFiles update(ConFiles conFiles) {
        Project project = projectMapper.selectById(conFiles.getProjId());
        String projName = project.getProjName();

        User user = userMapper.selectById(conFiles.getUserId());
        Long depId = user.getDepId();
        conFiles.setDeptId(depId);
        this.getBaseMapper().updateById(conFiles);
        return this.queryById(conFiles.getId());
    }

    @Override
    public List<ConFiles> querryList() {
        return this.getBaseMapper().selectList(null);
    }

    @Override
    public boolean deleteById(Long id) {
        return this.getBaseMapper().deleteById(id) > 0;
    }


    private void dealConFile(ConFiles file){

        // 处理项目
        Project project = projectMapper.selectById(file.getProjId());
        file.setProject(project);

        User user = userMapper.selectById(file.getUserId());
        Long depId = file.getDeptId();
        file.setUser(user);
        Depart depart = departMapper.selectById(depId);
        file.setDepart(depart);
        // 处理文件数量
        Long count = fileMapper.selectCount(new QueryWrapper<File>()
                .eq("no_id", file.getId())
                .eq("type", 1));
        file.setFileCount(count);

        // 携带文件
        List<File> fileList= fileService.getFiles(1,file.getId());
        if(CollectionUtils.isNotEmpty(fileList)){
            file.setFileList(fileList);
        }else {
            file.setFileList(new ArrayList<File>());
        }

    }
}
